namespace utils{
    export class DateTimeUtils {
        /**一天的秒数 */
        public static DAY_SEC:number = 86400;
        /**一小时的秒 */
        public static HOUR_SEC:number = 3600;
        /**一分钟的秒数 */
        public static MIN_SEC:number = 60;
        
        private static Y_REG: RegExp = /Y+/g;
        private static M_REG: RegExp = /M+/g;
        private static D_REG: RegExp = /D+/g;
    
        private static d_REG: RegExp = /d+/g; //天
        private static h_REG: RegExp = /h+/g;
        private static i_REG: RegExp = /i+/g;
        private static s_REG: RegExp = /s+/g;

        private static YMD_REGS:RegExp[] = [DateTimeUtils.Y_REG,DateTimeUtils.M_REG,DateTimeUtils.D_REG];
        private static DHIS_REGS:RegExp[] = [DateTimeUtils.d_REG,DateTimeUtils.h_REG,DateTimeUtils.i_REG,DateTimeUtils.s_REG];
        private static DHIS_SECS:number[] = [DateTimeUtils.DAY_SEC,DateTimeUtils.HOUR_SEC,DateTimeUtils.MIN_SEC,0];
        /**
         * 格式化时间日期
         * @param dateOrSec 可以是秒数或者Date对象;想格式化YMD 只能传Date对象 
         * @param fmt  格式:Y-M-D h:i:s 或 Y-MM-DD hh:ii:ss Y代表年固定显示四位数,M,D d,h,i,s;代表：月日 天时分秒,传两个会转换为01格式
         * @param isSlice 是否不显示值为0的格式;例如: 1天00时00分30秒；true会返回 1天30秒
         */
        public static format(dateOrSec: any, fmt: string = "hh:ii:ss",isSlice:boolean=false): string {
            if(dateOrSec instanceof Date){
                fmt =this.formatDate(dateOrSec,fmt,isSlice);
            }else if(typeof(dateOrSec)=="number"){
                fmt =this.formatSec(dateOrSec,fmt,isSlice);
            }            
            console.log("fmt",fmt);            
            return fmt;
        }

        private static formatSec(second:number,fmt: string = "hh:ii:ss",isSlice:boolean=false):string{            
            fmt = this.fmtDHIS(fmt,second,isSlice);
            return fmt
        }

        /**         
         * @param date 
         * @param fmt 
         */
        private static formatDate(date:Date,fmt:string="Y-MM-DD hh:ii:ss",isSlice:boolean=false):string{            
            fmt = this.fmtYMD(date,fmt);
            //根据日期格式化 不会格式化天
            fmt = this.fmtDHIS(fmt,[-1,date.getHours(),date.getMinutes(),date.getSeconds()],isSlice);
            return fmt;
        }

        private static fmtYMD(date: Date, fmt: string): string {
            let ymdRegs:RegExp[] = this.YMD_REGS;
            let ymds:number[] = [date.getFullYear(),date.getMonth() + 1,date.getDate()];
            for(let i:number=0;i<ymdRegs.length;i++){
                let reg:RegExp = ymdRegs[i];
                reg.lastIndex = 0;
                let regExc: RegExpExecArray = reg.exec(fmt);
                if(regExc){
                    fmt = this.replaceFmt(regExc[0], ymds[i] + "",fmt);
                }
            }
            return fmt;
        }
    
        private static fmtDHIS(fmt: string,secOrVals:any,isSlice:boolean=false): string {  
            let regArr:RegExp[] = this.DHIS_REGS;
            let secs:number[]= this.DHIS_SECS;
            let dhisVals:number[];
            let second:number = 0;
            if(secOrVals instanceof Array){
                dhisVals = secOrVals;
            }else{
                second = secOrVals;
            }
            for(let i:number=0;i<regArr.length;i++){
                regArr[i].lastIndex = 0;
                let reg:RegExpExecArray = regArr[i].exec(fmt);
                if(reg){
                    let tar:number = 0; 
                    if(!dhisVals){
                        //自行计算
                        tar = secs[i]>0?Math.floor(second/secs[i]):second;
                        second = second%secs[i];
                    }else{
                        tar = dhisVals[i];//直接传参替换 
                    }
                    if(isSlice && tar<=0 ){            
                        fmt = this.sliceFmt(fmt,i+1,reg[0],regArr);                                            
                    }else{
                        fmt = this.replaceFmt(reg[0],tar+"",fmt);
                    }                    
                }   
            }          
            return fmt;
        }
        /**
         * 剔除值为0的格式
         * @param fmt 
         * @param j 
         * @param curFmt 
         * @param regArr 
         */
        private static sliceFmt(fmt:string,j:number,curFmt:string,regArr:RegExp[]):string{
            let stIdx:number = fmt.indexOf(curFmt);
            let endIdx:number = -1;
            let nextExc:RegExpExecArray;
            while(j<regArr.length){                
                regArr[j].lastIndex = 0;
                nextExc = regArr[j].exec(fmt);
                if(nextExc){
                    endIdx = fmt.indexOf(nextExc[0]);
                    break;
                }
                j++;
            }
            let fix:string = fmt.slice(0,stIdx);
            let end:string = endIdx>-1?fmt.slice(endIdx):"";
            fmt =  fix + end;
            return fmt;
        }
        private static replaceFmt(regStr: string, ydStr: string, fmt: string): string {
            if (regStr.length > ydStr.length) {
                ydStr = "0" + ydStr;
            }
            fmt = fmt.replace(regStr, ydStr);
            return fmt;
        }
    
    }
}


